#!/usr/bin/perl
#
# usage:
#   wget --post-data='node1 node2 node3' -q -O - http://localhost/cgi-bin/limura/Game/destroyNode.cgi'?USER1'
#
#   JSON
#     {
#       "Request":"DestroyNode",
#       "UserName":"USER1",
#       "Nodes":[
#         "node1", "node2", "node3"
#       ]
#     }

require 'config.ph';

while(<>){
	s/\r?\n$//;
	$nodes .= " ". $_;
}
if($nodes =~ m/\{/){
	$jsonObj = from_json($nodes);
	if(defined($jsonObj) && defined($jsonObj->{"Request"}) && $jsonObj->{"Request"} eq 'DestroyNode'){
		$userName = $jsonObj->{"UserName"};
		$nodeList = $jsonObj->{"Nodes"};
		$nodes = join(' ', @$nodeList);
	}else{
		$nodes = '';
	}
}
if($nodes =~ m/^\s*$/){
	&PrintError("Request format error.");
	exit(0);
}
$masterURL = &GetMasterURL($userName);
if($masterURL eq ''){
	&PrintError("userName '$userName' not registered.");
	exit(0);
}
$result = join('', `$WGET -q -O - --post-data='$nodes' $masterURL/destroyNodes.cgi'?'$userName`);
if($result =~ m/^success/){
	&PrintResult("$nodes destroyed.");
}else{
	&PrintError($result);
}
exit(0);
